package com.redis.sample.common.controller;

import com.redis.sample.rate.limit.RateLimit;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("rate")
public class SampleController {

  private AtomicLong num = new AtomicLong(0);

  /**
   * http://localhost:8081/rate/sliding?userNo=1
   * @param userNo
   * @return
   */
  @RateLimit(value=5, duration=10, limitKey="userNo")
  @GetMapping(value = "sliding")
  public Map<String, Object> sliding(@RequestParam String userNo){
    Map<String, Object> map = new HashMap<>();
    map.put("userNo", userNo);
    map.put("num", num.incrementAndGet());
    return map;
  }

}
